<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\mill\model\Order as OrderModel;
use app\mill\model\Sample as SampleModel;
use app\wh\model\Material as MaterialModel;
use think\Db;

/**
 * 合同表模型
 */
class Contract extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mill_order_contract';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //状态
    public static $status = [
        -1 =>     '未通过',
        0 =>      '待提交',
        1 =>      '待审核',
        2 =>      '待财务审核',
        3 =>      '已审核',
    ];

    // 表格制作
    //print 0:编辑模式 1:打印模式
    public static function fromTabale($data = [],$print = 0){


        $table_head = [];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;' class='td-none  td-50'>签发：</div>",
            "[5:1]<div class='td-none'><input type='hidden' name='order_id' value='{$data['order_id']}'><input type='hidden' name='order_pid' value='{$data['order_pid']}'></div>",
            "[3:1]<div style='text-align: right;' class='td-none text-bottom'>合同编号：{$data['contract_sn']}</div><input type='hidden' name='contract_sn' value='{$data['contract_sn']}'>",
        ];
        $table_data[] = [
            "[9:1]<div style='text-align: center;letter-spacing: 20px;' class='td-none td-bottom'><h2>合同任务书</h2></div>",
        ];

        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>客户名称：</div>",
            "[4:1]{$data['company']}",
            "<div style='text-align: center;font-weight: bold;'>销售经理：</div>",
            "[3:1]".SampleModel::inputToText('sales',$data['sales'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>工程名称：</div>",
            "[4:1]".SampleModel::inputToText('engineering_full',$data['engineering_full'],$print),
            "<div style='text-align: center;font-weight: bold;'>工程简称：</div>",
            "[3:1]".SampleModel::inputToText('engineering',$data['engineering'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>技术负责人：</div>",
            "[4:1]".SampleModel::inputToText('director',$data['director'],$print),
            "<div style='text-align: center;font-weight: bold;'>技术负责人电话：</div>",
            "[3:1]".SampleModel::inputToText('director_mobile',$data['director_mobile'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>收货人：</div>",
            "[4:1]".SampleModel::inputToText('address_nickname',$data['address_nickname'],$print),
            "<div style='text-align: center;font-weight: bold;'>收货人电话：</div>",
            "[3:1]".SampleModel::inputToText('address_mobile',$data['address_mobile'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>工程编号：</div>",
            "[4:1]".SampleModel::inputToText('order_sn',$data['order_sn'],$print),
            "<div style='text-align: center;font-weight: bold;'>面积(m²)：</div>",
            "[3:1]".SampleModel::inputToText('area',$data['area'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>产品规格：</div>",
            "[8:1]".SampleModel::inputToText('spec',$data['spec'],$print),
        ];

        if(!empty($print)){
            foreach ($data['contract'] as $rk=>$rv_arr){
                $require_table = "[7:1]<div style='text-align: left;'>";
                foreach ($rv_arr as $rvv){
                    $require_table .= "<label class='css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded'>
            <input type='checkbox' name='contract[{$rk}][]' value='{$rvv}' checked><span></span>{$rvv}</label></br>";
                }
                $require_table .= "</div>";
                $table_data[] = [
                    "<div style='text-align: center;font-weight: bold;width: 125px;'>{$rk}：</div>",
                    $require_table]
                ;
            }
        }
        else{
            $require = config('mill_contract_contract');
            foreach ($require as $rk=>$rv){
                $require_table1 = "<div style='width: 120px;text-align: center;font-weight: bold;'>{$rk}：</div>";
                $require_table2 = '';
                $rv_arr = explode('|||',$rv);
                foreach ($rv_arr as $rkk=>$rvv){

                    $checked = '';
                    $rvv_html = $rvv;
                    //input框
                    if(strpos($rvv,'input') !== false){
                        $val_arr = explode('input',$rvv);

                        //获取多选里额外的input文本框的值
                        $val = '';
                        if(!empty($data['contract'][$rk]))$val = SampleModel::otherInputValue($val_arr,$data['contract'][$rk]);

                        //缓存该多选的值
                        $i = 0;
                        $val_cache = $val;
                        $needle = '|||';
                        $rvv_html = str_replace("input",$needle,$rvv);
                        //判断该多选是否有值
                        $in_val = mb_strlen($val_cache) > mb_strlen(implode("",$val_arr))?true:false;

                        //获取input值，并拼接
                        foreach ($val_arr as $val_k=>$val_v){

                            if($val_k>0){

                                if($in_val){
                                    //获取第一个input值出现的位子
                                    $sub_val = $val_cache;
                                    if(!empty($val_v))$sub_val = mb_substr($val_cache,0,mb_strpos($val_cache, $val_v));
                                    //去除第一个input值出现的位子，并缓存
                                    $len0 = mb_strlen($sub_val);
                                    $len1 = mb_strlen($val_cache)-$len0;
                                    $val_cache = mb_substr($val_cache,$len0,$len1);
                                    //获取第一个input值
                                    $val_v_fist = $val_arr[$val_k-1];
                                    $len0 = mb_strlen($val_v_fist);
                                    $len1 = mb_strlen($sub_val)-$len0;
                                    $val = mb_substr($sub_val,$len0,$len1);

                                    $checked = 'checked';
                                }

                                //拼接
                                $input = "</label>";
                                $input .= SampleModel::inputToText("contract_input[{$rk}][$rvv][$i]",$val,$print,'width: 210px;display: inline;');
                                $input .= "<label class='css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded'>";

                                $rvv_html = substr_replace($rvv_html,$input,strpos($rvv_html,$needle),strlen($needle));
                                $i++;
                            }
                        }

                    }

                    if(!empty($data['contract'][$rk]) && in_array($rvv,$data['contract'][$rk]))$checked = 'checked';

                    //多选框
                    $require_table2 .= "<label class='css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded text-height'>
            <input type='checkbox' name='contract[{$rk}][]' value='{$rvv}' {$checked}><span></span>{$rvv_html}</label></br>";

                }

                $table_data[] = [$require_table1,"[8:1]
            {$require_table2}"];
            }
        }

        //收款方式
        //$financeTable = self::financeTable($data,$print);
        if(!empty($financeTable)){
            $table_data[] = [
                "<div style='text-align: center;font-weight: bold;'>收款方式：</div>",
                "[9:1]
                ".$financeTable['html_type'],
            ];
            $table_data[] = [
                "",
                "[9:1]
                ".$financeTable['html_config'],
            ];
        }

        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>其他说明：</div>",
            "[9:1]".SampleModel::inputToText('note',$data['note'],$print,'height: 120px;','textarea'),
        ];

        $table_data[] = [
            "[9:1]<div style='font-weight: bold;    text-align: right;' class='td-none td-top td-50'><div style='float: left;width: 15%;'>部门审核：</div><div style='float: left;text-align: left;width: 15%;'>{$data['group_verify']}&nbsp;</div><div style='float: left;width: 17%;'>主管经理审核：</div><div style='float: left;width: 21%;'>&nbsp;</div><div style='float: left;width: 15%;'></div><div style='float: left;width: 15%;'>&nbsp;</div></div>",
        ];
        $table_data[] = [
            "[9:1]<div style='font-weight: bold;    text-align: right;' class='td-none td-top td-50'><div style='float: left;width: 15%;'>技术工艺部：</div><div style='float: left;width: 15%;'>&nbsp;</div><div style='float: left;width: 17%;'>开料生产部：</div><div style='float: left;width: 21%;'>&nbsp;</div><div style='float: left;width: 15%;'>折弯生产部：</div><div style='float: left;width: 15%;'>&nbsp;</div></div>",
        ];
        $table_data[] = [
            "[9:1]<div style='font-weight: bold;    text-align: right;' class='td-none td-top td-50'><div style='float: left;width: 15%;'>技术研发中心：</div><div style='float: left;width: 15%;'>&nbsp;</div><div style='float: left;width: 17%;'>制表人：</div><div style='float: left;width: 21%;text-align: left'>{$data['compile']}<input type='hidden' name='compile' value='{$data['compile']}'></div><div style='float: left;width: 15%;'>制表日期：</div><div style='float: left;width: 15%;text-align: left'>".date('Y-m-d',$data['update_time'])."<input type='hidden' name='update_time' value='{$data['update_time']}'></div></div>",
        ];

        return ['head'=>$table_head,'data'=>$table_data];
    }

    // 表格制作
    //print 0:编辑模式 1:打印模式
    public static function fromTabale_old($data = [],$print = 0){


        $table_head = [];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;' class='td-none  td-50'>签发：</div>",
            "[5:1]<div class='td-none'><input type='hidden' name='order_id' value='{$data['order_id']}'><input type='hidden' name='order_pid' value='{$data['order_pid']}'></div>",
            "[3:1]<div style='text-align: right;' class='td-none text-bottom'>合同编号：{$data['contract_sn']}</div><input type='hidden' name='contract_sn' value='{$data['contract_sn']}'>",
        ];
        $table_data[] = [
            "[9:1]<div style='text-align: center;letter-spacing: 20px;' class='td-none td-bottom'><h2>合同任务书</h2></div>",
        ];

        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>加价部分：</div>",
            "[9:1]
<a class='btn btn- btn-default pop' href='".url('mill/contract/addPrice',['order_pid'=>$data['order_pid']])."?_pop=1' data-layer=\"{&quot;area&quot;:[&quot;90%&quot;,&quot;70%&quot;],&quot;title&quot;:[&quot;加价&quot;,&quot;font-size:24px;text-align: center;&quot;]}\" _tag=\"show\" data-toggle=\"tooltip\" data-original-title=\"加价\"><i class=\"\"></i>点击查看</a>",
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>客户名称：</div>",
            "[4:1]{$data['company']}",
            "<div style='text-align: center;font-weight: bold;'>销售经理：</div>",
            "[3:1]".SampleModel::inputToText('sales',$data['sales'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>工程名称：</div>",
            "[4:1]".SampleModel::inputToText('engineering_full',$data['engineering_full'],$print),
            "<div style='text-align: center;font-weight: bold;'>工程简称：</div>",
            "[3:1]".SampleModel::inputToText('engineering',$data['engineering'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>技术负责人：</div>",
            "[4:1]".SampleModel::inputToText('director',$data['director'],$print),
            "<div style='text-align: center;font-weight: bold;'>技术负责人电话：</div>",
            "[3:1]".SampleModel::inputToText('director_mobile',$data['director_mobile'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>收货人：</div>",
            "[4:1]".SampleModel::inputToText('address_nickname',$data['address_nickname'],$print),
            "<div style='text-align: center;font-weight: bold;'>收货人电话：</div>",
            "[3:1]".SampleModel::inputToText('address_mobile',$data['address_mobile'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>工程编号：</div>",
            "[4:1]".SampleModel::inputToText('order_sn',$data['order_sn'],$print),
            "<div style='text-align: center;font-weight: bold;'>面积(m²)：</div>",
            "[3:1]".SampleModel::inputToText('area',$data['area'],$print),
        ];
        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>产品规格：</div>",
            "[8:1]".SampleModel::inputToText('spec',$data['spec'],$print),
        ];

        if(!empty($print)){
            foreach ($data['contract'] as $rk=>$rv_arr){
                $require_table = "[7:1]<div style='text-align: left;'>";
                foreach ($rv_arr as $rvv){
                    $require_table .= "<label class='css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded'>
            <input type='checkbox' name='contract[{$rk}][]' value='{$rvv}' checked><span></span>{$rvv}</label></br>";
                }
                $require_table .= "</div>";
                $table_data[] = [
                    "<div style='text-align: center;font-weight: bold;width: 125px;'>{$rk}：</div>",
                    $require_table]
                ;
            }
        }
        else{
            $require = config('mill_contract_contract');
            foreach ($require as $rk=>$rv){
                $require_table1 = "<div style='width: 120px;text-align: center;font-weight: bold;'>{$rk}：</div>";
                $require_table2 = '';
                $rv_arr = explode('|||',$rv);
                foreach ($rv_arr as $rkk=>$rvv){

                    $checked = '';
                    $rvv_html = $rvv;
                    //input框
                    if(strpos($rvv,'input') !== false){
                        $val_arr = explode('input',$rvv);

                        //获取多选里额外的input文本框的值
                        $val = '';
                        if(!empty($data['contract'][$rk]))$val = SampleModel::otherInputValue($val_arr,$data['contract'][$rk]);

                        //缓存该多选的值
                        $i = 0;
                        $val_cache = $val;
                        $needle = '|||';
                        $rvv_html = str_replace("input",$needle,$rvv);
                        //判断该多选是否有值
                        $in_val = mb_strlen($val_cache) > mb_strlen(implode("",$val_arr))?true:false;

                        //获取input值，并拼接
                        foreach ($val_arr as $val_k=>$val_v){

                            if($val_k>0){

                                if($in_val){
                                    //获取第一个input值出现的位子
                                    $sub_val = $val_cache;
                                    if(!empty($val_v))$sub_val = mb_substr($val_cache,0,mb_strpos($val_cache, $val_v));
                                    //去除第一个input值出现的位子，并缓存
                                    $len0 = mb_strlen($sub_val);
                                    $len1 = mb_strlen($val_cache)-$len0;
                                    $val_cache = mb_substr($val_cache,$len0,$len1);
                                    //获取第一个input值
                                    $val_v_fist = $val_arr[$val_k-1];
                                    $len0 = mb_strlen($val_v_fist);
                                    $len1 = mb_strlen($sub_val)-$len0;
                                    $val = mb_substr($sub_val,$len0,$len1);

                                    $checked = 'checked';
                                }

                                //拼接
                                $input = "</label>";
                                $input .= SampleModel::inputToText("contract_input[{$rk}][$rvv][$i]",$val,$print,'width: 210px;display: inline;');
                                $input .= "<label class='css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded'>";

                                $rvv_html = substr_replace($rvv_html,$input,strpos($rvv_html,$needle),strlen($needle));
                                $i++;
                            }
                        }

                    }

                    if(!empty($data['contract'][$rk]) && in_array($rvv,$data['contract'][$rk]))$checked = 'checked';

                    //多选框
                    $require_table2 .= "<label class='css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded text-height'>
            <input type='checkbox' name='contract[{$rk}][]' value='{$rvv}' {$checked}><span></span>{$rvv_html}</label></br>";

                }

                $table_data[] = [$require_table1,"[8:1]
            {$require_table2}"];
            }
        }

        //收款方式
        //$financeTable = self::financeTable($data,$print);
        if(!empty($financeTable)){
            $table_data[] = [
                "<div style='text-align: center;font-weight: bold;'>收款方式：</div>",
                "[9:1]
                ".$financeTable['html_type'],
            ];
            $table_data[] = [
                "",
                "[9:1]
                ".$financeTable['html_config'],
            ];
        }

        $table_data[] = [
            "<div style='text-align: center;font-weight: bold;'>其他说明：</div>",
            "[9:1]".SampleModel::inputToText('note',$data['note'],$print,'height: 120px;','textarea'),
        ];

        $table_data[] = [
            "[9:1]<div style='font-weight: bold;    text-align: right;' class='td-none td-top td-50'><div style='float: left;width: 15%;'>部门审核：</div><div style='float: left;text-align: left;width: 15%;'>{$data['group_verify']}&nbsp;</div><div style='float: left;width: 17%;'>主管经理审核：</div><div style='float: left;width: 21%;'>&nbsp;</div><div style='float: left;width: 15%;'></div><div style='float: left;width: 15%;'>&nbsp;</div></div>",
        ];
        $table_data[] = [
            "[9:1]<div style='font-weight: bold;    text-align: right;' class='td-none td-top td-50'><div style='float: left;width: 15%;'>技术工艺部：</div><div style='float: left;width: 15%;'>&nbsp;</div><div style='float: left;width: 17%;'>开料生产部：</div><div style='float: left;width: 21%;'>&nbsp;</div><div style='float: left;width: 15%;'>折弯生产部：</div><div style='float: left;width: 15%;'>&nbsp;</div></div>",
        ];
        $table_data[] = [
            "[9:1]<div style='font-weight: bold;    text-align: right;' class='td-none td-top td-50'><div style='float: left;width: 15%;'>技术研发中心：</div><div style='float: left;width: 15%;'>&nbsp;</div><div style='float: left;width: 17%;'>制表人：</div><div style='float: left;width: 21%;text-align: left'>{$data['compile']}<input type='hidden' name='compile' value='{$data['compile']}'></div><div style='float: left;width: 15%;'>制表日期：</div><div style='float: left;width: 15%;text-align: left'>".date('Y-m-d',$data['update_time'])."<input type='hidden' name='update_time' value='{$data['update_time']}'></div></div>",
        ];

        return ['head'=>$table_head,'data'=>$table_data];
    }

    // 表格制作 合同加价
    //print 0:编辑模式 1:打印模式
    public static function fromAddPrice($data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        $table_head = [];
        $table_data = [];

        //批量打印
        $btn_html = "";
        $btn_html .= "<a style='float: right;' class='btn btn-success' onclick=\"goods_add()\" ><i class='fa fa-plus-circle'></i></a>";

        $table_data[] = [
            "<div style='text-align: center;'>名称</div>",
            "<div style='text-align: center;'>公式1</div>",
            "<div style='text-align: center;'>值1</div>",
            "<div style='text-align: center;'>关系</div>",
            "<div style='text-align: center;'>公式2</div>",
            "<div style='text-align: center;'>值2</div>",
            "<div style='text-align: center;'>加价</div>",
            "<div id='goods_note'><div style='text-align: center;float: left;margin: 7px auto;'></div>".$btn_html."</div>",
        ];

        foreach ($data['lists'] as $sak=>$sav){
            if(!empty($print) && 1==2){
                $table_data[] = [
                    "<div style='text-align: center;'></div>",
                ];
            }
            else{
                //$is_add = $print;
                //if(empty($sav['id']) || $sav['status'] == 2)$is_add = 1;
                $readonly = "equa";
                if($sav['type'] == 0)$readonly .= ' readonly';
                $table_data[] = [
                    "[0:1]
                    <input class='form-control goods' type='hidden' name='goods[id][]' value='{$sav['id']}'>
                    <input class='form-control goods' type='hidden' name='goods[order_id][]' value='{$sav['order_id']}'>
                    <input class='form-control goods' type='hidden' name='goods[archives_sn][]' value='{$sav['archives_sn']}'>
                    <input class='form-control goods' type='hidden' name='goods[create_time][]' value='{$sav['create_time']}'>
                    <input class='form-control goods' type='hidden' name='goods[archives_field][]' value='{$sav['archives_field']}'>
                    <input class='form-control goods' type='hidden' name='goods[note][]' value='{$sav['note']}'>
                    <input class='form-control goods' type='hidden' name='goods[title][]' value='{$sav['title']}'>
                    
                    ".MaterialModel::inputToText('goods[type_id][]',$sav['type_id'],$print,'','select','goods not-js-select',$data['type_list_arr']),
                    "[0:1]
                    ".MaterialModel::inputToText('goods[equa][]',$sav['equa'],$print,'','select',"goods {$readonly} not-js-select",$data['equa_arr']),
                    "[0:1]
                    ".MaterialModel::inputToText('goods[val][]',$sav['val'],$print,'','input',"goods {$readonly}"),
                    "[0:1]
                    ".MaterialModel::inputToText('goods[type][]',$sav['type'],$print,'','select',"goods {$readonly} not-js-select",$data['type_arr']),
                    "[0:1]
                    ".MaterialModel::inputToText('goods[equa2][]',$sav['equa2'],$print,'','select',"goods {$readonly} not-js-select",$data['equa_arr']),
                    "[0:1]
                    ".MaterialModel::inputToText('goods[val2][]',$sav['val2'],$print,'','input',"goods {$readonly}"),
                    "[0:1]
                    ".MaterialModel::inputToText('goods[price][]',$sav['price'],$print,'','input','goods'),
                    "[0:1]
                    <a class='btn btn-danger goods goods_del' onclick=\"goods_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                ];
            }


        }

        //设置表单项
        //$from[] = ['complexTable:12', 'order', '', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------

        //打印模式
        //if(!empty($print))return ['head'=>$table_head,'data'=>$table_data];
        return ['head'=>$table_head,'data'=>$table_data];
    }

    //收款方式
    public static function financeTable($data = [],$print = 0){

        if($print == 1) return '';
        //收款方式，用于财务板块
        $finance_arr = [
            //先款后货
            //到一批，结算一批
            [
                'title'                     =>  '先款后货',
                'val'                       =>  0,
                'html'                      =>  "到一批，结算一批",
                'config'                    =>  [],
            ],
            //按月对账，并结算
            //发货后，第_2_个月_20_日对账，第_3_个月_30_日前支付_80_%，余下_20_%累计到下一批发货单结算
            [
                'title'                     =>  '按月对账，并结算',
                'val'                       =>  1,
                'html'                      =>  "发货后，第__recon_month__个月__recon_day__日对账，第__pay_month__个月__pay_day__日前支付__pay_money_scale__%，余下__pay_tomoney_scale__%累计到下一批发货单结算",
                'config'                    =>  [//配置
                    //对账
                    'recon_month'           =>  2,//第_2_个月
                    'recon_day'             =>  20,//第_month_个月_20_日
                    //收款
                    'pay_month'             =>  3,//第_3_个月
                    'pay_day'               =>  30,//第_month_个月_30_日
                    'pay_money_scale'       =>  80,//支付_80_%
                    'pay_tomoney_scale'     =>  20,//余下_20_%累计到下一批发货单结算
                ]
            ],
            //发货进度款
            //累计达到进度款_60_万元
            //不管_2_个月内是否达标，也要在_10_天内对账
            //收到发票后，_5_天内支付总货款的_80_%，剩余_20_%不记入下次进度款
            //设置最终收款时间
            [
                'title'                     =>  '发货进度款',
                'val'                       =>  2,
                'html'                      =>  "累计达到进度款__recon_money__万元，不管__recon_month__个月内是否达标，也要在__recon_day_num__天内对账。收到发票后，__pay_day_num__天内支付总货款的__pay_money_scale__%。最终收款时间__pay_end_pay_time__",
                'config'                    =>  [//配置
                    //对账
                    'recon_month'           =>  2,//第_2_个月
                    'recon_day_num'         =>  20,//第_month_个月的_10_天内对账
                    'recon_money'           =>  60,//累计达到进度款_60_万元
                    //收款
                    'pay_day_num'           =>  5,//收到发票后，_5_天内
                    'pay_money_scale'       =>  80,//总货款的_80_%
                    //'pay_tomoney_scale'     =>  0,//余下_20_%累计到下一批发货单结算
                    'pay_end_pay_time'      =>  date('Y-m-d'),//最终收款时间
                ]
            ],
            //月结
            //设置月结_75_%
            //当月货款，第_2_个月前对账，第_2_个月_10_前支付_75_%
            //全部发完货后，结算剩余_97_%，_180_天后结算剩余_3_%
            [
                'title'                     =>  '月结',
                'val'                       =>  3,
                'html'                      =>  "月结__pay_money_scale__%。当月货款，第__recon_month__个月前对账，第__pay_month__个月__pay_day_num__前支付__pay_money_scale__%。全部发完货后，结算剩余__pay_end_pay_scale__%，__pay_end_pay_num__天后结算剩余款项",
                'config'                    =>  [//配置
                    //对账
                    'recon_month'           =>  2,//第_2_个月
                    'recon_day_num'         =>  1,//第_month_个月的_1_天内对账
                    //收款
                    'pay_month'             =>  2,//第_2_个月
                    'pay_day_num'           =>  10,//第_month_个月的_10_号
                    'pay_money_scale'       =>  75,//月结_75_%
                    'pay_tomoney_scale'     =>  0,//余下_20_%累计到下一批发货单结算
                    'pay_end_pay_scale'     =>  97,//全部发完货后，结算剩余_97_%
                    'pay_end_pay_num'       =>  180,//全部发完货后，结算剩余_97_%，_180_天结算剩余_3_%
                ]
            ],
            //货到付款
            //货到工地7日内付款
            [
                'title'                     =>  '货到付款',
                'val'                       =>  4,
                'html'                      =>  "货到后__pay_day_num__日内付款，规定日期__pay_end_date_y__年__pay_end_date_m__月__pay_end_date_d__日后开始",
                'config'                    =>  [//配置
                    //收款
                    'pay_day_num'           =>  7,//货到工地7日内付款
                    'pay_end_date_y'        =>  date('Y'),//年，2023年2月份后供货按货到工地7日内付款
                    'pay_end_date_m'        =>  date('m'),//月，2023年2月份后供货按货到工地7日内付款
                    'pay_end_date_d'        =>  date('d'),//日，2023年2月份后供货按货到工地7日内付款
                ]
            ],
            //项目供货完毕后付款
            //项目供货完毕（距最后一车发货之日起20天内无新订单则视为供货完毕）的30天内付清该项目全部货款
            [
                'title'                     =>  '供货完毕后付款',
                'val'                       =>  5,
                'html'                      =>  "项目供货完毕（距最后一车发货之日起20天内无新订单则视为供货完毕）的__pay_day_num__天内付清该项目全部货款",
                'config'                    =>  [//配置
                    //收款
                    'pay_day_num'           =>  30,//货到工地7日内付款
                ]
            ],
            //押一付一
            //采用押一车货款的支付方式，即乙方发第一车货到甲方工地后，暂不付款，乙方在送第二车货之前，甲方支付乙方第一车货的全额货款
            [
                'title'                     =>  '押一付一',
                'val'                       =>  6,
                'html'                      =>  "采用押一车货款的支付方式，即乙方发第一车货到甲方工地后，暂不付款，乙方在送第二车货之前，甲方支付乙方第一车货的全额货款，
乙方在送第三车货之前，甲方支付乙方第二车货的全额货款，以此类推。最后一车货采取先款后货的付款当时",
                'config'                    =>  [//配置
                    //收款
                    //'pay_day_num'           =>  30,//货到工地7日内付款
                ]
            ],
            //按方量付款
            //供货达2000㎡为一付款节点，到达付款节点后7天内完成供货部分货款核对，并支付2000㎡的全额货款，以此类推
            [
                'title'                     =>  '按方量付款',
                'val'                       =>  7,
                'html'                      =>  "供货达__pay_area_num__㎡为一付款节点，到达付款节点后__pay_day_num__天内完成供货部分货款核对，并支付__pay_area_num__㎡的全额货款，以此类推",
                'config'                    =>  [//配置
                    //收款
                    'pay_area_num'          =>  2000,//供货达2000㎡为一付款节点
                    'pay_day_num'           =>  7,//到达付款节点后7天内付款
                ]
            ],
            //方量月结或进度款
            //3000㎡结算一次或每月底结算上月供货款
            //收到发票后，支付批次结算货款额的_80_%，剩余_20_%不记入下次进度款
            [
                'title'                     =>  '方量月结或进度款',
                'val'                       =>  8,
                'html'                      =>  "供货数量每达到__pay_area_num__㎡结算一次或每__recon_month__个月__recon_day_num__号结算上月供货款（以先到达为准），收到发票后,付至批次结算货款额的__pay_money_scale__%，以此类推。
",
                'config'                    =>  [//配置
                    //对账
                    'recon_month'           =>  1,//第_2_个月
                    'recon_day_num'         =>  28,//第_month_个月的_20_天内对账
                    'pay_area_num'          =>  3000,//累积达到3000㎡结算一次
                    //收款
                    'pay_money_scale'       =>  80,//总货款的_80_%
                    //'pay_tomoney_scale'     =>  0,//余下_20_%累计到下一批发货单结算
                ]
            ],
            //按累积款
            //累计达到进度款_100_万元，在_5_天内对账
            [
                'title'                     =>  '按累积款',
                'val'                       =>  9,
                'html'                      =>  "乙方为甲方供货金额每累计到达__recon_money__万元为一付款节点，到达付款节点__pay_day_num__个工作日内支付供货金额的__pay_money_scale__%。最终收款时间__pay_end_pay_time__",
                'config'                    =>  [//配置
                    //对账
                    'recon_money'           =>  100,//累计达到进度款_100_万元
                    //收款
                    'pay_day_num'           =>  5,//收到发票后，_5_天内
                    'pay_money_scale'       =>  100,//总货款的_80_%
                    //'pay_tomoney_scale'     =>  0,//余下_20_%累计到下一批发货单结算
                    'pay_end_pay_time'      =>  date('Y-m-d'),//最终收款时间
                ]
            ],
            //按预付款
            //甲方支付乙方_5_万元作为材料预付款，预付款在第一次结算支付时一次性扣除；
            //材料到场发票移交甲方之日起_7_个工作日付该批货物全款:；结算金额达合同量_80_%时，结算扣留_5_万元，供货完成无质量色差问题，_2_个月内把剩余货款全部付清给乙方。
            //_2023-1-20_内供货金额超过预付款的，超出部分须在_2023-1-20_前结清。
            [
                'title'                     =>  '按预付款',
                'val'                       =>  10,
                'html'                      =>  "甲方支付乙方__advance_money__万元作为材料预付款，预付款在第一次结算支付时一次性扣除；收到发票后，__pay_day_num__天内付该批货物全款；结算金额达合同量__pay_money_scale__%时，结算扣留__retention_money__万元，供货完成无质量色差问题，__retention_month__个月内把剩余货款全部付清给乙方。__pay_end_pay_time__内供货金额超过预付款的，超出部分须在__pay_end_pay_time__前结清。",
                'config'                    =>  [//配置
                    //对账
                    'recon_money'           =>  100,//累计达到进度款_100_万元
                    //收款
                    'advance_money'           =>  5,//甲方支付乙方_5_万元作为材料预付款
                    'pay_day_num'           =>  5,//收到发票后，_5_天内
                    'pay_money_scale'       =>  80,//结算金额达合同量_80_%
                    'retention_money'       =>  5,//结算金额达合同量__80__%时，结算扣留__5__万元
                    'retention_month'       =>  2,//__2__个月内把剩余货款全部付清给乙方
                    //'pay_tomoney_scale'     =>  0,//余下_20_%累计到下一批发货单结算
                    'pay_end_pay_time'      =>  date('Y-m-d'),//最终收款时间
                ]
            ],

        ];

        $finance_html = [
            'html_type'     => '',
            'html_config'   => '',
        ];
        //定金
        $finance_dj = $finance_html['html_config'] .= "定金：".SampleModel::inputToText("finance_money",$data['finance_money'],$print,'width: 130px;display: inline;margin-bottom: 10px;')."元。滞纳金".SampleModel::inputToText("late_money",$data['late_money'],$print,'width: 130px;display: inline;margin-bottom: 10px;')."元，滞纳金退还日期".SampleModel::inputToText("late_time",$data['late_time'],$print,'width: 130px;display: inline;margin-bottom: 10px;');

        foreach ($finance_arr as $v){
            $checked = '';
            $hidden = 'hidden';
            if($v['val'] == $data['finance_type']){
                $checked = "checked";
                $hidden = '';
            }
            //收款方式
            $finance_html['html_type'] .= "<label class='css-input css-radio css-radio-primary css-radio-sm push-10-r'><input type='radio' name='finance_type' value='{$v['val']}' {$checked}><span></span> {$v['title']}</label>";
            //收款设置
            foreach ($v['config'] as $ck=>$cv){

                $val = isset($data['finance_config'][$v['val']][$ck])?$data['finance_config'][$v['val']][$ck]:$cv;

                $width = '80px';
                if(strpos($ck,'time'))$width = '130px';
                $input = SampleModel::inputToText("finance_config[{$v['val']}][{$ck}]",$val,$print,"width: {$width};display: inline;");
                $v['html'] = str_replace("__{$ck}__",$input,$v['html']);
            }
            //打印模式
            if($print > 0){
                $finance_html['html_config'] = $finance_dj;
                if($v['val'] == $data['finance_type']){
                    $finance_html['html_config'] .= "<br>{$v['html']}";
                    break;
                }
            }
            //编辑模式
            else $finance_html['html_config'] .= "<div class='finance {$hidden}' data-type='{$v['val']}'>{$v['html']}</div>";
        }

        return $finance_html;
    }

    // 打印模板
    public static function toHtml($data = [],$print = 1){

        //缩放比例
        $table_scaling = 1;
        $table_style = 'border="1" style="border-collapse: collapse;width: '.(21/$table_scaling).'cm;-webkit-transform: scale('.$table_scaling.');transform-origin: 0 0;"';
        $html = '<html><style>th{font-size: 18px;font-weight: bold;}td{}</style><body style="margin: 4px;">';

        //头部
        $html .= '<table '.$table_style.'><thead>
                        <tr><th colspan="9" style="font-size: 21px;font-weight: bold;"><img style="width:50px;margin-bottom: -22px;" class="td-none" src="'.config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo2.jpg">'.config('web_site_title').'<br><div style="font-size: 10px; margin-left: 48px;">Chongqing New Silaya Aluminum Industry(Group) Co., Ltd..</div></th></tr>
                    ';
        $table_arr = self::fromTabale($data,$print);
        foreach ($table_arr as $tk=>$tv){
            //头
            if($tk == 'head'){
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = OrderModel::tableParseCell($tvvv);

                        $html .= '
                            <th rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</th>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</thead>';
            }
            //体
            elseif($tk == 'data'){
                $html .= '<tbody>';
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = OrderModel::tableParseCell($tvvv);

                        $html .= '
                            <td rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</td>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</tbody>';
            }
        }
        $html .= '</table>';

        //js
        $html .= '
                    <script src="'.config('template.tpl_replace_string')['__ADMIN_JS__'].'/core/jquery.min.js?v=20180327"></script>
                    <script type="text/javascript">
                        //去除边框
                        $(".td-none").parent().css("border-style","hidden")
                        //设置头部边框
                        $(".td-top").parent().css("border-top","black");
                        //设置底部边框
                        $(".td-bottom").parent().css("border-bottom","black");
                        //设置高度
                        $(".td-50").parent().css("height","50px");
                        //设置向下对齐
                        $(".text-bottom").parent().css("vertical-align","bottom");
                        //设置文字上下间距
                        $(".text-height").parent().css("line-height","28px");
                        
                        window.print();
                    </script>';

        return $html;
    }


}
